﻿using mortal_ClipboardSharing_windows.model;
using mortal_ClipboardSharing_windows.user.register;
using System;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Navigation;

namespace mortal_ClipboardSharing_windows
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<MyItem> ItemList { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            ItemList = new ObservableCollection<MyItem>();
            this.listBox.DataContext = this.ItemList;
        }

        ClipboardHooker m_clipboardHooker;

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            m_clipboardHooker = new ClipboardHooker(this);
            m_clipboardHooker.ClipboardUpdated += OnClipboardUpdated;
        }

        private void OnClipboardUpdated(object sender, EventArgs e)
        {
            string time = DateTime.Now.ToString();
            IDataObject data = Clipboard.GetDataObject();
            if (data.GetDataPresent(DataFormats.Text) | data.GetDataPresent(DataFormats.OemText))
            {
                //检测文本
                string text = (String)data.GetData(DataFormats.Text);
                ItemList.Add(new MyItem(text, time));

                // 
                Api.AddText(text, delegate(HttpResult<string> httpResult)
                {
                    Console.WriteLine(httpResult.Code);
                });
            }
            else if (data.GetDataPresent(DataFormats.Bitmap))
            {
                //检测图像
                //pictureBox1.Image = Clipboard.GetImage();
                //MyItem item = new MyItem();
                //item.CopyToClipboard();
            }
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            if (m_clipboardHooker == null) return;
            m_clipboardHooker.ClipboardUpdated -= OnClipboardUpdated;
            m_clipboardHooker.Dispose();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Register register = new Register();
            register.Owner = this;
            register.PassValuesEvent += new Register.PassValuesHandler(ReceiveValues);
            register.ShowDialog();
        }

        // 接收登录窗体传过来的值
        private void ReceiveValues(object sender, PassValuesEventArgs e)
        {
            //this.tbValue1.Text = e.Value1;
            //this.tbValue2.Text = e.Value2.ToString();

            Console.WriteLine("token: " + e.Token);
            Console.WriteLine("nickname: " + e.Nickname);
        }
    }

    class ClipboardHooker : IDisposable
    {
        const int WM_CLIPBOARDUPDATE = 0x031D;

        [DllImport("User32.dll")]
        static extern bool AddClipboardFormatListener(IntPtr hwnd);

        HwndSource _hwndSource = null;

        public void Dispose()
        {
            _hwndSource?.Dispose();
        }

        public ClipboardHooker(Window window)
        {
            WindowInteropHelper helper = new WindowInteropHelper(window);
            _hwndSource = HwndSource.FromHwnd(helper.Handle);
            bool r = AddClipboardFormatListener(_hwndSource.Handle);
            if (r)
            {
                _hwndSource.AddHook(new HwndSourceHook(OnHooked));
            }
        }

        private IntPtr OnHooked(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == WM_CLIPBOARDUPDATE)
            {
                ClipboardUpdated?.Invoke(this, EventArgs.Empty);
                return IntPtr.Zero;
            }
            return IntPtr.Zero;
        }

        public event EventHandler ClipboardUpdated;
    }

    public class MyItem
    {
        public MyItem()
        {

        }
        public MyItem(string name, string time)
        {
            this.Name = name;
            this.Time = time;
        }

        public string Name { get; set; }
        public string Time { get; set; }
    }
}
